//任务管理


//站点id
var siteid;

// 任务ID
var taskid;

// 分页索引：
var index = 1;

// 每页显示数目
var pagenum = 9;

// 总数目
var count;
// 页数
var pages;

// 操纵站点下拉框时，为任务下拉框赋相应的值
$(document).ready(function() {
	// 当操作站点下拉框时，把值赋给siteid
	$("#selectwebsite").change(function() {
		siteid = $(this).attr('value');
		displaytaskname(siteid);
		$("#selecttask").html("<option value=\"null\" >请先选择任务</option>");
		$("#displaytaskdata").html("");
		$("#fenye").css("display","none");
	});
});

// 操作任务下拉框时，显示出相应的任务的url
$(document).ready(function() {
	// 当操作任务下拉框时，把值赋给taskid
	$("#selecttask").change(function() {
		count=0;
		taskid = $(this).attr('value');
		if(taskid == "null"){
			taskid =null;
			$("#nonedata").css("display", "block");
			$("#displaytaskdata").html("");
			$("#fenye").css("display","none");			
		}else{
			$("#displaytaskdata").css("display","block");
			$("#fenye").css("display","block");
			onetaskurlcont(taskid,index,pagenum);
		}
	});
});

// 第一次加载时显示的分页
function fenye(count){
	
	var intcount = parseInt(count);
	var numhtml="";
	var counthtml;
	// 计数器
	var num = 10;
	if(count<pagenum){
		pages = 1;		
		numhtml = "<li><a href=\"javascript:void(0)\" >"+1+"</a></li>";
		counthtml = "当前第1页/共1页"+count+"条数据";
	}else{
		if(count%pagenum !=0){
						 
			pages = parseInt(intcount/pagenum);
			
			pages = pages+1
		}else{
			pages = intcount/pagenum;
		}
		
		counthtml = "当前第"+index+"页/共"+pages+"页"+count+"条数据";
		
		pageshu = pages+1;
		
		var dangqianpage = this.index;
		if(dangqianpage < num){
			for(var i=1;i<num;i++){
				if(i<pageshu){
					if(i==this.index){
						numhtml += "<li id=\"fenyexuanzhong\">"+i+"</li>";
					}else{
					numhtml += "<li><a href=\"javascript:void(0)\" onclick=\"indexpage("+i+")\" >"+i+"</a></li>";
					}
				}
			}
		}	
	}
	
	$("#fenyenum").html(numhtml);
	$("#dangqian_count").html(counthtml);
}

function twofenye(index){
	
	var prev = index-5;
	var next = index+6;
	
	var numhtml="";
	
	counthtml = "当前第"+index+"页/共"+pages+"页"+count+"条数据";
	
	for(var i = prev;i<index;i++){
		if(i>0){
			numhtml += "<li><a href=\"javascript:void(0)\" onclick=\"indexpage("+i+")\" >"+i+"</a></li>";
		}
	}
	
	for(var a=index;a<next;a++){
		if(a<pages+1){
			if(a==index){
				numhtml += "<li id=\"fenyexuanzhong\">"+i+"</li>";
			}else{
				numhtml += "<li><a href=\"javascript:void(0)\" onclick=\"indexpage("+a+")\" >"+a+"</a></li>";
			}	
		}
		
	}
	if(index<3){
		$("#shouye").css("display","none");
		$("#prevpage").css("display","none");
	}else{
		$("#shouye").css("display","block");
		$("#prevpage").css("display","block");
		$("#nextpage").css("display","block");
		$("#lastpage").css("display","block");
	}
	if(index<pages+1 && index>pages-10 ){
		$("#nextpage").css("display","none");
		$("#lastpage").css("display","none");

	}
	
	$("#fenyenum").html(numhtml);
	$("#dangqian_count").html(counthtml);
//	secondtaskurlcont(taskid,index,pagenum);
}

function indexpage(index){	
	this.index = index;
	if(index<9){
		onetaskurlcont(siteid,index,pagenum);
	}else{
		secondtaskurlcont(taskid,index,pagenum);
	}
}

function shouye(){
	this.index = 1;
	fenye(count);
	$("#shouye").css("display","none");
	$("#prevpage").css("display","none");
	$("#nextpage").css("display","block");
	$("#lastpage").css("display","block");
	secondtaskurlcont(taskid,index,pagenum);
}

function lastpage(){
	lastpage = this.pages;
	this.index = lastpage;
	$("#shouye").css("display","block");
	$("#prevpage").css("display","block");
	$("#nextpage").css("display","none");
	$("#lastpage").css("display","none");
	secondtaskurlcont(taskid,index,pagenum);
}

function prevpage(){
	this.index = index-1;
	secondtaskurlcont(taskid,index,pagenum);
}

function nextpage(){
	this.index = index+1;
	secondtaskurlcont(taskid,index,pagenum);

}

function onetaskurlcont(taskid,index,pagenum) {
	var displayhtml = "<table id=\"displaytable\" cellspacing=\"0\" ><tbody><tr><th width=\"80%\" style=\"border-left:1px solid #DBDBDB;\">URL</th><th width=\"20%\">操作</th></tr>";
	$.ajax({
		type:"post",
		url:"tool/taskurl/displaytaskurl.jsp",
		dataType:"json",
		data:{
			taskid:taskid,
			index:index,
			pagenum:pagenum
		},
		success:function(responseText, textStatus){
			var jsondata = eval(responseText);
			for ( var i = 0; i < jsondata.length; i++) {
				displayhtml += "<tr><td class=\"tableleftborder\">"
						+ jsondata[i].taskurl+ "</td>"								
						+ "<td><a href=\"javascript:void(0)\"  onclick=\"displaymodifictaksurl('"+jsondata[i].urlid+"')\" class=\"modif\">修改</a>&nbsp;"
						+ "<a href=\"javascript:void(0)\" onclick=\"deletetaskurl('"+jsondata[i].urlid+"')\">删除</a></td></tr>";
				if(i==jsondata.length-1){
					count = jsondata[i].count;					
				}
			}
			displayhtml += "</tbody></table>";
			$("#nonedata").css("display", "none");
			$("#displaytaskdata").html(displayhtml);
			if(count == null){
				count=0;
				
			}
			fenye(count);
		}		
})
}

function secondtaskurlcont(taskid,index,pagenum) {
	var displayhtml = "<table id=\"displaytable\" cellspacing=\"0\" ><tbody><tr><th width=\"80%\" style=\"border-left:1px solid #DBDBDB;\">URL</th><th width=\"20%\">操作</th></tr>";
	$.ajax({
		type:"post",
		url:"tool/taskurl/displaytaskurl.jsp",
		dataType:"json",
		data:{
			taskid:taskid,
			index:index,
			pagenum:pagenum
		},
		success:function(responseText, textStatus){
			var jsondata = eval(responseText);
			for ( var i = 0; i < jsondata.length; i++) {
				displayhtml += "<tr><td class=\"tableleftborder\">"
						+ jsondata[i].taskurl+ "</td>"								
						+ "<td><a href=\"javascript:void(0)\"  onclick=\"displaymodifictaksurl('"+jsondata[i].urlid+"')\" class=\"modif\">修改</a>&nbsp;"
						+ "<a href=\"javascript:void(0)\" onclick=\"deletetaskurl('"+jsondata[i].urlid+"')\">删除</a></td></tr>";
				if(i==jsondata.length-1){
					count = jsondata[i].count;
					
				}
			}
			displayhtml += "</tbody></table>";				 
			$("#nonedata").css("display", "none");
			$("#displaytaskdata").html(displayhtml);
			twofenye(index);
		},
	})
}

// 删除任务URL
function deletetaskurl(urlid){
	$.ajax({
		type:"post",
		url:"tool/taskurl/deletetaskurl.jsp",
		dataType:"text",
		data:{
			urlid:urlid,
		},
		success : function(issuccess, textStatus) {
			alert(issuccess);
			if(index<9){
				onetaskurlcont(taskid,index,pagenum);
			}else{
				secondtaskurlcont(taskid,index,pagenum);
			}
		}
	})
}

// 显示修改url页面
function displaymodifictaksurl(urlid){
	$.ajax({
		type:"post",
		url:"tool/taskurl/displaymodifictaskurl.jsp",
		dataType:"text",
		data:{
			urlid:urlid
		},
		success : function(responseText, textStatus) {
			var jsonresult=eval("(" + responseText + ")");
			popupDiv('displaymodifictaskurl')
			$("#cjpeizhi").val(jsonresult.taskurl);			
			$("#pzurlid").val(jsonresult.urlid);
			
		}
	})
}
// 修改任务url
function modifictaskurl(){
	urlid = $("#pzurlid").attr("value");
	var urllink = $("#cjpeizhi").attr("value");
	
	urllink = urllink.replace(/(^\s*)|(\s*$)/g,"");
	if(urllink==""){
		alert("URL为空，无法修改");
	}else{
		$.ajax({
			type:"post",
			url:"tool/taskurl/modifictaskurl.jsp",
			dataType:"text",
			data:{
				urlid:urlid,
				urllink:urllink
			},
			success : function(issuccess, textStatus) {
				alert(issuccess);
				onetaskurlcont(taskid,index,pagenum)
				hideDiv('displaymodifictaskurl');
				 
				
			}
		})
	}
}


// 添加url
function addtaskurl(){
	var urllink = $("#addtasklink").attr("value");
	
	urllink = urllink.replace(/(^\s*)|(\s*$)/g,"");
	if(urllink==""){
		alert("有为空的选项，无法添加");
	}else{	
		$.ajax({
			type:"post",
			url:"tool/taskurl/addtaskurl.jsp",
			dataType:"text",
			data:{
				taskid:taskid,
				urllink:urllink
			},
			success : function(issuccess, textStatus) {
				alert(issuccess);
				
				hideDiv('displaynewtaskurl');
				$("#addtasklink").val(" ");
				if(index<9){
					onetaskurlcont(taskid,index,pagenum);
				}else{
					secondtaskurlcont(taskid,index,pagenum);
				}
			}
		})
	}
}
// 显示任务名字
function displaytaskname(siteid){
	var displayhtml="";
	$.ajax({
		type:"post",
		url:"tool/taskurl/gettasknamebysiteid.jsp",
		dataType:"json",
		data:{
			siteid:siteid
		},
		success : function(responseText, textStatus) {
			
			var jsonresult = eval(responseText);

			for ( var i = 0; i < jsonresult.length; i++) {
				displayhtml+= "<option value=\""+jsonresult[i].taskid+"\" >"+jsonresult[i].taskname+"</option>";
			}
			$("#nonedata").css("display", "none");
			$("#selecttask").append(displayhtml);
		 
		},
	})
}

// // 显示某站点某任务下的url
// function displaytaskUrl(taskid) {
// var displayhtml = "<table id=\"displaytable\" cellspacing=\"0\"
// ><tbody><tr><th>URL</th><th>操作</th></tr>";
// $.ajax({
// type:"post",
// url:"tool/taskurl/displaytaskurl.jsp",
// dataType:"json",
// data:{
// taskid:taskid
// },
// success:function(responseText, textStatus){
// var jsondata = eval(responseText);
// for ( var i = 0; i < jsondata.length; i++) {
// displayhtml += "<tr><td class=\"tableleftborder\">"
// + jsondata[i].taskurl+ "</td>"
// + "<td><a href=\"javascript:void(0)\"
// onclick=\"displaymodifictaksurl('"+jsondata[i].urlid+"')\"
// class=\"modif\">修改</a>&nbsp;"
// + "<a href=\"javascript:void(0)\"
// onclick=\"deletetaskurl('"+jsondata[i].urlid+"')\">删除</a></td></tr>";
// }
// displayhtml += "</tbody></table>";
// $("#nonedata").css("display", "none");
// $("#displaytaskdata").html(displayhtml);
//				 
// },
// })
// }

// 显示添加任务窗口：首先判断，是否选择某站点，如未选择弹窗提示
// 选择则显示添加任务窗口
function displayaddtaskurl() {
	if (siteid == null) {
		alert("请先选择站点再进行相关操作");		
	}else {
		if(taskid == null){
			alert("请先选择站点，然后选择任务后再进行相关操作");	
		}else{
			popupDiv('displaynewtaskurl')
			
		}
				
	}
}
